package com.smartguard.websocket;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

import com.alibaba.fastjson.JSON;
import com.smartguard.bean.ClientEvent;
import com.smartguard.common.Const;
import com.smartguard.util.MessageUtil;

@ServerEndpoint("/sgwebsocket/{did}")
public class SGWebSocket {

	private static final Map<String, Session> sessions = new ConcurrentHashMap<String, Session>();
	// private static DeviceService deviceService = new DeviceService();
	// private static UserInfoService UserInfoService = new UserInfoService();

	private static String loc = null;
	private static String re = null;

	@OnMessage
	public void onMessage(String message, Session session) throws IOException, InterruptedException {
		System.out.println("Received: " + message);

		// 接下来是收到有关视频直播信息的处理
		String temp = message.substring(0, 7);

		if (temp.equals("{\"cal\":")) {
			loc = (String) session.getUserProperties().get("did");
			int l = message.length();
			re = message.substring(8, l - 2);
			System.out.println("send cal to " + re);
			sessions.get(re).getBasicRemote().sendText(message);
		} else if (temp.equals("{\"sdp\":")) {
			String may = (String) session.getUserProperties().get("did");
			if (may.equals(loc)) {
				System.out.println(loc + " send sdp to " + re);
				sessions.get(re).getBasicRemote().sendText(message);
			} else {
				System.out.println(re + " send sdp to " + loc);
				sessions.get(loc).getBasicRemote().sendText(message);
			}
		} else {

			// 处理客户端发送过来的消息。

			ClientEvent clientEvent = (ClientEvent) JSON.parseObject(message, ClientEvent.class);

			// if (EventHandler.checkMonitor(clientEvent.getDeviceHash()) ==
			// false) {
			// return;
			// }

			switch (clientEvent.getEventType()) {
			// 加速度监控事件
			case Const.EVENT_SENSOR:
				System.out.println("加速度感应");
				EventHandler.handleSensor(clientEvent);
				break;
			// 视频事件
			case Const.EVENT_VIDEO:

				break;

			default:
				break;
			}

			// 设备加速度感应器感应到门或窗异常
			// 给同一个用户所有设备发送信息
			// if(clientEvent.getEnventType() == 1){
			// MessageUtil.sendMessageForSensor(clientEvent.getDeviceHash());
			// }

			if (clientEvent.getEventType() == 7) {
				System.out.println(clientEvent.getDeviceHash() + ":" + clientEvent.getEventType());
				MessageUtil.sendNessageTest();
			}
		}
	}

	@OnOpen
	public void onOpen(Session session, @PathParam(value = "did") String did) {
		System.out.println(did + " connected, ");
		// session 增加属性，did
		session.getUserProperties().put("did", did);
		if (sessions.get(did) != null) {
			session.getOpenSessions().remove(session);
		}

		sessions.put(did, session);
		System.out.println("the number of connection " + sessions.size());

	}

	@OnClose
	public void onClose() {
		System.out.println("Connection closed");
		// TODO delete the useless sessions
	}

	public static Map<String, Session> getSessions() {
		return sessions;
	}

}
